package com.example.hello.views.users;

import com.example.hello.*;
import com.example.hello.POJO.BadgePOJO;
import com.example.hello.POJO.UserPOJO;
import com.example.hello.TextField;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener;
import com.vaadin.ui.*;

import java.util.List;

public class EditUser extends VerticalLayout implements View {
    
    public static final String NAME = "edit_user";

    public static UserPOJO currentUser;
    
    private VerticalLayout top = new VerticalLayout();
    private HorizontalLayout mid = new HorizontalLayout();
    private HorizontalLayout bot = new HorizontalLayout();
    
    private TextField userNameField = new TextField();
    private TextField firstNameField = new TextField();
    private TextField lastNameField = new TextField();
    private TextField emailField = new TextField();

    protected UserPOJO user;
    protected List<BadgePOJO> badges;

    @Override
    public void enter(ViewChangeListener.ViewChangeEvent event) {
        String[] parameters = event.getParameters().split("/");

        // only site admins allowed
        if (!((HelloUI) HelloUI.getCurrent()).getUser().isSiteAdmin())
            getUI().getNavigator().navigateTo(Users.NAME + "/" + parameters[0]);

        if (!parameters[0].equals("")) user = User.GETUser(parameters[0]);

        if (user != null) {
            userNameField.setValue(user.getUserName());
            firstNameField.setValue(user.getFirstName());
            lastNameField.setValue(user.getLastName());
            emailField.setValue(user.getEmail());
        }
    }

    public EditUser() {
        setWidth("1000px");
        setHeight("300px");

        initTop();
        initMid();
        initBot();

        // add to parent
        addComponents(
                top,
                new VSpacer(20),
                mid,
                new VSpacer(20),
                bot
        );
        setExpandRatio(mid, 1);
    }

    private void initTop() {
        top.setHeight("80px");
        top.addStyleName("border-bottom");

        final Heading userNameHeading = new Heading("Username", 3);
        userNameField.setWidth("134px");

        // add to top
        top.addComponents(userNameHeading, userNameField);
        top.setComponentAlignment(userNameField, Alignment.BOTTOM_LEFT);
        top.setExpandRatio(userNameHeading, 3);
        top.setExpandRatio(userNameField, 2);
    }

    private void initMid() {
        mid.setHeight("100px");

        final Heading firstNameHeading = new Heading("First Name", 4);
        final Heading lastNameHeading = new Heading("Last Name", 4);
        final Heading emailHeading = new Heading("Email", 4);

        // add to mid
        mid.addComponents(
                new VerticalLayout(firstNameHeading, firstNameField),
                new HSpacer(20),
                new VerticalLayout(lastNameHeading, lastNameField),
                new HSpacer(20),
                new VerticalLayout(emailHeading, emailField)
        );
    }
    
    private void initBot() {
        BootstrapButton back = new BootstrapButton("Back");
        BootstrapButton submit = new BootstrapButton("Submit");

        back.addListener(new Listener() {
            @Override
            public void componentEvent(Event event) {
                HelloUI.getCurrent().getNavigator().navigateTo(Users.NAME + "/" + user.getEncodedName());
            }
        });
        submit.addListener(new Listener() {
            @Override
            public void componentEvent(Event event) {
                String oldUserName = user.getEncodedName();
                String newUserName = userNameField.getEncodedValue();

                user.setFirstName(firstNameField.getEncodedValue());
                user.setLastName(lastNameField.getEncodedValue());
                user.setEmail(emailField.getValue());
                User.changeUserName(user, oldUserName, newUserName);

                ((HelloUI) HelloUI.getCurrent()).setUser(user);
                ((HelloUI) HelloUI.getCurrent()).setLogInOutFlag(true);

                user.set_id(null);
                HttpRequest.executePut(Users.NAME + "/" + oldUserName, user.toJSON());

                HelloUI.getCurrent().getNavigator().removeView(Users.NAME + "/" + oldUserName);
                HelloUI.getCurrent().getNavigator().navigateTo(Users.NAME + "/" + newUserName);
            }
        });

        bot.addComponents(back, new HSpacer(20), submit);
    }

}